import { sys } from 'cc';
import engine from '../Engine';
import Singleton from '../util/Singleton';
import { AdsBase } from './AdsBase';
import { KSRecorder, kuaishouAdsFunc } from './platform/KUAISHOU/KuaiShouAdsFunc';
import { TTAdsFunc, TTRecorder } from './platform/TT/TTAdsFunc';
import { RecorderBase } from './RecorderBase';
import { CCConst } from './util/CCConst';
import { CCCPadding } from './util/CCCPadding';
import { CCGlobal, VideoCall as adCall } from './util/CCGlobal';
import { WeiXinAdsFunc } from './platform/WEIXIN/WeiXinAdsFunc';

export class AdsFunc extends Singleton<AdsFunc>() {

    private _sourceFunc: AdsBase = null;

    private _recorder: RecorderBase = null;

    /**录屏 */
    public get recorder() {
        if (this._recorder) {
            return this._recorder;
        }
        switch (CCGlobal.PLATFORM) {
            case CCConst.PLATFORM.KS:
                this._recorder = new KSRecorder();
                this._recorder.init();
                break;
            case CCConst.PLATFORM.TT:
                this._recorder = new TTRecorder();
                this._recorder.init();
                break;
        }
        return this._recorder;
    }


    /**初始化 */
    public init(data: { platform: string, GameType: string }) {

        if (sys.isBrowser) { engine.cc_debug = true }
        CCGlobal.PLATFORM = data.platform;
        CCGlobal.API = window[data.platform];
        CCGlobal.GameType = data.GameType;
        let config = CCCPadding[data.GameType][data.platform];
        if (!!config) {
            CCGlobal.Version = config.version;
            CCGlobal.Config.videoId = config.videoId;
            CCGlobal.Config.appId = config.appId;
            CCGlobal.Config.project = config.project
            CCGlobal.Config.interstitialId = config.interstitialId;
        }
        if (CCGlobal.PLATFORM == CCConst.PLATFORM.WEB) {
            engine.cc_debug = true;
        }
        !engine.cc_debug && this.sourceFunc.init();


        engine.log.trace(`
            gameType:${CCGlobal.GameType}\n
            platform:${CCGlobal.PLATFORM}\n
            version:${CCGlobal.Version}\n
            isDebug:${engine.cc_debug}
            `)
    }

    private get sourceFunc() {
        if (this._sourceFunc) {
            return this._sourceFunc;
        }
        switch (CCGlobal.PLATFORM) {
            case CCConst.PLATFORM.KS:
                this._sourceFunc = new kuaishouAdsFunc();
                break;
            case CCConst.PLATFORM.TT:
                this._sourceFunc = new TTAdsFunc();
                break;
            case CCConst.PLATFORM.WEIXIN:
                this._sourceFunc = new WeiXinAdsFunc();
        }
        return this._sourceFunc;
    }

    /**是否需要广告 */
    private _getCanUse() {
        return engine.cc_debug;
    }

    /**创建视频广告 */
    public creatorVideoAd(obj: adCall) {
        if (this._getCanUse()) {
            obj.suCallback && obj.suCallback();
            return;
        };
        this.sourceFunc && this.sourceFunc.loadVideoAd && this.sourceFunc.loadVideoAd(obj);
    }

    /**侧边栏 */
    public toSidebar(obj: adCall) {
        if (this._getCanUse()) { return obj.suCallback && obj.suCallback() };
        this.sourceFunc && this.sourceFunc.toSidebar && this.sourceFunc.toSidebar(obj);
    }

    /**分享 */
    public shareAppMessage(obj: adCall) {
        if (this._getCanUse()) { return obj.suCallback && obj.suCallback() };
        this.sourceFunc.shareAppMessage(obj);
    }

    /**添加桌面 */
    public addShortcut(obj: adCall) {
        if (this._getCanUse()) { return obj.suCallback && obj.suCallback() };
        this.sourceFunc.addShortcut(obj);
    }

    /**插屏 */
    public createInterstitial(obj: adCall) {
        if (this._getCanUse()) { return obj.suCallback && obj.suCallback() };
        this.sourceFunc && this.sourceFunc.createInterstitial && this.sourceFunc.createInterstitial(obj);
    }


}

